package cn.gson.school.controller.jiuye;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiuye.InterviewExperienceEntityDao;
import cn.gson.school.entity.jiuye.InterviewExperienceEntity;
import cn.gson.school.mappers.jiuye.InterviewExperienceEntityMapper;
/**
 * 资料经验操作
 * @author 华少
 *
 */
@Controller
@RequestMapping("/jiuye")
public class InterviewExperienceController {
	@Autowired
	private InterviewExperienceEntityDao interviewExperienceDao;
	@Autowired
	private InterviewExperienceEntityMapper interviewExperienceMapper;

	@RequestMapping("jingyan")
	@ResponseBody
	public Map<String, Object> index(InterviewExperienceEntity interviewexperience,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 配置分页
		PageHelper.startPage(pageNum, pageSize);
		// 查询数据
		List<InterviewExperienceEntity> listDate = interviewExperienceMapper.list(interviewexperience);

		PageInfo<InterviewExperienceEntity> info = new PageInfo<>(listDate);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		InterviewExperienceEntity interviewExperienceEntity;
		if(id !=null&&(interviewExperienceEntity=interviewExperienceDao.findOne(id))!=null) {
			interviewExperienceDao.delete(interviewExperienceEntity);
			obj.put("success", true);
		}else {
			obj.put("message", "此用户不存在！");
		}
		return obj;  
	}
/**
 * 修改
 * @param id
 * @param model
 * @return
 */
	@GetMapping({ "jy-ms-tjjingyan", "jy-ms-tjjingyan/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			InterviewExperienceEntity u = interviewExperienceDao.findOne(id);
			if (u != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				String a=u.getInterviewExperienceAnswer();
				String q=u.getInterviewExperienceQuestion();
				String t=u.getQuestionType();
				Long uid=u.getInterviewExperienceId();
				model.addAttribute("answer", a);
				model.addAttribute("question", q);
				model.addAttribute("type", t);
				model.addAttribute("user", uid);
			}
		}
		return "jiuye/jy-ms-tjjingyan";
	}

	/**
	 * 添加
	 * 
	 * @param interviewExperienceEntity
	 * @param br
	 * @return
	 */
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> save(@Valid InterviewExperienceEntity interviewExperienceEntity, BindingResult br) {
		Map<String, Object> obj = new HashMap<String, Object>();
		if(interviewExperienceEntity.getUserInfoId()==null) {
			System.out.println(interviewExperienceEntity);
			obj.put("message", "useridfoid为空！！！");
			return obj;
		}
		obj.put("success", false);
			if(interviewExperienceEntity.getInterviewExperienceId() == null) {
				if ("".equals(interviewExperienceEntity.getInterviewExperienceQuestion())) {
					obj.put("message", "问题不能为空！");
					return obj;
				} else if ("".equals(interviewExperienceEntity.getInterviewExperienceAnswer())) {
					obj.put("message", "答案不能为空！");
					return obj;
				}
			}else {  
				interviewExperienceEntity.setUserInfoId(interviewExperienceEntity.getUserInfoId());
				interviewExperienceEntity.setInterviewExperienceAnswer(interviewExperienceEntity.getInterviewExperienceAnswer());
				interviewExperienceEntity.setInterviewExperienceQuestion(interviewExperienceEntity.getInterviewExperienceQuestion());
				interviewExperienceEntity.setQuestionType(interviewExperienceEntity.getQuestionType());
			}
			interviewExperienceEntity.setInterviewExperienceTime(new Date());
			interviewExperienceDao.save(interviewExperienceEntity);
			obj.put("success", true);
		return obj;
	}

}
 